<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <div class="w-full mt-2 mb-2">
      <div
        v-for="(item, index) in dataRef"
        :key="index"
        class="flex items-center gap-2 mb-2 w-2/3 m-auto"
      >
        <div
          class="overflow-hidden whitespace-nowrap overflow-ellipsis w-100px text-right"
          :title="item.title"
        >
          {{ item.title }}
        </div>
        <div v-if="item.fieldType == 7 || item.fieldType == 8">
          从
          <DatePicker v-model:value="item.fromDate" value-format="YYYY-MM-DD" format="YYYY-MM-DD" />
          至
          <DatePicker v-model:value="item.toDate" value-format="YYYY-MM-DD" format="YYYY-MM-DD" />
        </div>
        <div v-else class="flex gap-2">
          <div v-if="item.fieldType == 0 || item.fieldType == 1">
            <Select :options="options2" v-model:value="item.cond" style="width: 80px" />
          </div>
          <div v-else>
            <Select
              v-if="item.fieldType != 4"
              :options="options3"
              v-model:value="item.cond"
              style="width: 80px"
            />
            <Select v-else :options="options1" v-model:value="item.cond" style="width: 80px" />
          </div>
          <div>
            <Input v-model:value="item.right" />
          </div>
        </div>
      </div>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getQueryGetSearchConditionsForScript } from '/@/api/reportForms/reportForms';
  import { Select, Input, DatePicker } from 'ant-design-vue';
  import { options1, options2, options3 } from './searchTable.data';
  export default defineComponent({
    name: 'SearchTableSearchModal',
    components: {
      BasicModal,
      Select,
      Input,
      DatePicker,
    },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const { createMessage } = useMessage();
      const isUpdate = ref(false);
      const dataRef = ref<Recordable>({});
      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        setModalProps({ confirmLoading: false, width: '800px', height: 500 });
        try {
          isUpdate.value = data?.isUpdate;
          const res = await getQueryGetSearchConditionsForScript({ id: data?.id });
          dataRef.value = res || [];
        } catch (err) {
          console.error(err);
        }
      });

      const getTitle = '搜索';

      async function handleSubmit() {
        try {
          if (dataRef.value.length == 0) {
            createMessage.warning('无条件数据');
            return;
          }
          const params = {};
          dataRef.value.forEach((item) => {
            if (item.fieldType == 7 || item.fieldType == 8) {
              params[`${item.name}FromDate`] = item.fromDate || '';
              params[`${item.name}ToDate`] = item.toDate || '';
            } else {
              params[`${item.name}_cond`] = item.cond || '';
              params[`${item.name}`] = item.right || '';
            }
          });
          setModalProps({ confirmLoading: true });
          createMessage.success('操作成功');
          closeModal();
          emit('success', params);
        } catch (err) {
          console.error(err);
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return {
        registerModal,
        getTitle,
        handleSubmit,
        dataRef,
        options1,
        options2,
        options3,
      };
    },
  });
</script>
